package dyy.zc.alumnibook.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.base.BaseFragment;
import dyy.zc.alumnibook.bean.FriendCircle;
import dyy.zc.alumnibook.bean.UserInfo;
import dyy.zc.alumnibook.ui.activity.ChangeInfoActivity;
import dyy.zc.alumnibook.ui.activity.CheckPhotoActivity;
import dyy.zc.alumnibook.ui.activity.PhotoActivity;
import dyy.zc.alumnibook.ui.adapter.FriendCircleAdapter;
import dyy.zc.alumnibook.utils.BmobUtils;
import dyy.zc.alumnibook.utils.ImageLoaderUtils;
import dyy.zc.alumnibook.utils.Utils;
import dyy.zc.alumnibook.views.CircleImageView;
import dyy.zc.alumnibook.views.EmptyView;

/**
 * 我的
 * Created by chuan on 2017/4/12.
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private TextView title,name,phone;
    private RelativeLayout back;
    private CircleImageView head;
    private ImageView change;
    private BmobUtils bmobUtils;
    private PullToRefreshListView pfl;
    private ListView listView;
    private List<FriendCircle> list = new ArrayList<>();
//    private List<PhotoAlbum> list = new ArrayList<>();
//    public PhotoAlbum photoAlbum;
    private FriendCircleAdapter adapter;
    private EmptyView emptyView;
//    private LoopRotarySwitchView mLoopRotarySwitchView;//旋转图片
//    public int width;//设置旋转图片的宽度
    private final int LIST_SUCCESS = 1;

    @Override
    protected View onCreateViews(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_mine,container,false);
        findViewsByIds(view);
        initData(loginuser);
        bind();
        return view;
    }

    private void findViewsByIds(View view) {
        bmobUtils = new BmobUtils(getActivity());
        pfl = Utils.findViewsById(view,R.id.recyclerView);
        pfl.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
        listView = pfl.getRefreshableView();

        emptyView = new EmptyView(view);

        back = Utils.findViewsById(view,R.id.top_rl_back);
//        mLoopRotarySwitchView = Utils.findViewsById(view,R.id.loopView);
        change = Utils.findViewsById(view,R.id.change);
        title = Utils.findViewsById(view,R.id.top_tv_title);
        name = Utils.findViewsById(view,R.id.name);
        phone = Utils.findViewsById(view,R.id.phone);
        head = Utils.findViewsById(view,R.id.head);
        title.setText("我的动态");
        back.setVisibility(View.GONE);
    }

    private void initData(UserInfo userInfo) {
        bmobUtils.queryPersonalCircle(userInfo,LIST_SUCCESS, handler);
        ImageLoaderUtils.initImage(getActivity(),userInfo.getHead().getFileUrl(),head,R.drawable.chat_xe_icon2_03);
        name.setText(userInfo.getName());
        phone.setText(userInfo.getAccount());
        Log.e("name",userInfo.getName());
        Log.e("phone",userInfo.getAccount());
    }

    /**
     * 初始化循环图片
     */
//    private void initLoopRotarySwitchView() {
//        DisplayMetrics dm = new DisplayMetrics();
//        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
//        windowManager.getDefaultDisplay().getMetrics(dm);
//        width = dm.widthPixels;
//        mLoopRotarySwitchView
//                .setR(width/2)//设置R的大小
//                .setAutoRotation(true)//是否自动切换
//                .setAutoRotationTime(2000);//自动切换的时间  单位毫秒
//    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void bind() {
        Utils.findViewsById(view,R.id.checkphoto).setOnClickListener(this);
        change.setOnClickListener(this);
        Utils.findViewsById(view,R.id.photo).setOnClickListener(this);
//        /**
//         * 选中回调
//         */
//        mLoopRotarySwitchView.setOnItemSelectedListener(new OnItemSelectedListener() {
//            @Override
//            public void selected(int position, View view) {
//
//            }
//        });
//
//        /**
//         * 触摸回调
//         */
//        mLoopRotarySwitchView.setOnLoopViewTouchListener(new OnLoopViewTouchListener() {
//            @Override
//            public void onTouch(MotionEvent event) {
//            }
//        });
//        /**
//         * 点击事件
//         */
//        mLoopRotarySwitchView.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(int item, View view) {
//
//
//            }
//        });
        adapter = new FriendCircleAdapter(getActivity(),list,getVmWidth());
        listView.setAdapter(adapter);

        pfl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                bmobUtils.queryPersonalCircle(loginuser,LIST_SUCCESS,handler);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LIST_SUCCESS:
                    Bundle bundle = msg.getData();
                    List<FriendCircle> friendCircles = (List<FriendCircle>) bundle.getSerializable("list");
                    if (friendCircles == null || friendCircles.size() == 0) {
                        //判断如果没有数据的话，则显示空提示
                        emptyView.setNotify("暂无动态");
                    } else {
                        list.clear();
                        list.addAll(friendCircles);
                        emptyView.setEmptyViewGone();
//                        Log.e("pho",photoAlbum.getImg().size()+"");
                        changeData();
//                    }
                        break;
                    }
            }
        }

    };

    /**
     * 添加数据
     */
    private void changeData() {

        adapter.setList(list);
        adapter.notifyDataSetChanged();
        pfl.onRefreshComplete();
    }
//        Log.e("asdf",photoAlbum.getImg().size()+"");
//        for (int i = 0; i < photoAlbum.getImg().size(); i++) {
//            View view = LayoutInflater.from(getActivity()).inflate(R.layout.photo_show, null);
//            ImageView image = (ImageView) view.findViewById(R.id.image);
//            ImageLoaderUtils.initImage(getActivity(), photoAlbum.getImg().get(i).getFileUrl(), image, R.mipmap.logo);
//            mLoopRotarySwitchView.addView(view);
//        }
//        initLoopRotarySwitchView();
//    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.change:
                Intent intent = new Intent(getActivity(), ChangeInfoActivity.class);
                startActivityForResult(intent,123);
                break;
            case R.id.photo:
                startActivity(new Intent(getActivity(), PhotoActivity.class));
                break;
            case R.id.checkphoto:
                startActivity(new Intent(getActivity(), CheckPhotoActivity.class) );
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 123:
                if (data != null) {
                    Bundle bundle = data.getExtras();
                    UserInfo info = (UserInfo) bundle.getSerializable("save");
                    initData(info);
                }
                break;
        }
    }
}
